package com.cs171.market.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cs171.market.Repository.CommentRepository;
import com.cs171.market.Repository.GoodRepository;
import com.cs171.market.entity.CartList;
import com.cs171.market.mapper.CommentMapper;
import com.cs171.market.entity.Comment;
import com.cs171.market.entity.Good;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@CrossOrigin
@RestController
public class CommentController {

    @Autowired
    private GoodRepository repository;
    @Autowired
    private CommentRepository commentRepository;
    @Autowired(required = false)
    private CommentMapper commentMapper;

    /**
     *1.通过id查找一个商品
     */
    @GetMapping("/good/{id}")
    public Object findById(@PathVariable("id")Integer id){
        System.out.println(repository.findById(id).orElse(null));
        Good good=repository.findById(id).orElse(null);
        String carousel=good.getCarousel();
        List<String> list =Arrays.asList(carousel.split(","));
        String choice=good.getChoices();
        List<String>  choiceList=Arrays.asList(choice.split(","));

        JSONObject dataJson=(JSONObject)JSON.toJSON(repository.findById(id).orElse(null).getMessage());
        JSONArray goodJson = (JSONArray) JSON.toJSON(list);
        dataJson.put("carousel",goodJson);
        JSONArray choiceJson = (JSONArray) JSON.toJSON(choiceList);
        dataJson.put("choice",choiceJson);

        return dataJson;
    }

    /**
     * 2.
     * @param gid
     * @return
     */
    @GetMapping("/comments/{id}")
    public List<Comment> findByCommentId(@PathVariable("id")Integer gid){
        System.out.println(commentMapper.getCommentsById(gid));
        return commentMapper.getCommentsById(gid);
    }

    @GetMapping("/specify/{id}")
     /**
     *
     * @param id
     * @return
     */
    public Object headerJson(@PathVariable("id")Integer id){
        Good good=repository.findById(id).orElse(null);
        String carousel=good.getCarousel();
        List<String> list =Arrays.asList(carousel.split(","));
        String choice=good.getChoices();
        List<String>  choiceList=Arrays.asList(choice.split(","));
        JSONObject dataJson=(JSONObject)JSON.toJSON(repository.findById(id).orElse(null).getMessage());
        JSONArray goodJson = (JSONArray) JSON.toJSON(list);
        dataJson.put("carousel",goodJson);
        JSONArray choiceJson = (JSONArray) JSON.toJSON(choiceList);
        dataJson.put("choice",choiceJson);


        List<Comment> test = commentMapper.getCommentsById(id);
        JSONArray commentJson=new JSONArray();
        for(Comment each:test)
        {
            commentJson.add(each.getMessage());
        }

//        JSONArray commentJson = (JSONArray) JSON.toJSON(commentMapper.getCommentsById(id));
        dataJson.put("comments",commentJson);

        JSONObject headerJson = new JSONObject();
        headerJson.put("success","true");
        headerJson.put("data", dataJson);
        return headerJson;
    }

    /**
     * 4.创建一条评论信息
     * @return
     */
    @PostMapping("/comment")
    public Comment create(@RequestBody JSONObject jsonParam)
    {
        String content = jsonParam.getString("content");
        Integer uid = jsonParam.getInteger("uid");
        Integer gid = jsonParam.getInteger("gid");
        Integer positive = jsonParam.getInteger("positive");
        Comment comment=new Comment();
        comment.setUid(uid);
        comment.setPositive(positive);
        comment.setContent(content);
        comment.setGid(gid);
        int ts = (int) (System.currentTimeMillis()/1000);
        comment.setTimeStamp(ts);
        return commentRepository.save(comment);
    }


}
